<?php

class QpostController extends Controller
{
	public function filters()
		{
			return array(
	              'accessControl',
			);
		}
	
		public function accessRules()
		{
			return array(
				array('allow',
	                'actions'=>array('add','list','edit','del','Categorylist','EditCategory','DelCategory','AddCategory'),
	                'roles'=>array('admin'),
			),
			
			array('deny',
	                'actions'=>array('add','list','edit','del','Categorylist','EditCategory','DelCategory','AddCategory'),
	                'users'=>array('*'),
			),
			);
		}
	
	
	
	
    public $layout = "application.views.layouts.admin";
    
    
 # View all post
    public function actionList() {
    	  $page_size =5; 
        $post = Post::model()->with('Coments')->recently(0,$page_size)->findAll();
        $post_count = Post::model()->count();
          $count_page=$post_count/$page_size;
     	if(!isset($_GET['page']))
         {         	
         		$post=Post::model()->recently(0,$page_size)->findAll();                   
         }
         else
           {
           	$page=$_GET['page'];
             $sdvig = $page_size*($page-1);
             $post=Post::model()->recently( $sdvig,$page_size)->findAll();
           }
        
        $this->render("application.views.qpost.list", array('post' => $post,'count_page'=>$count_page+1,));
    }
    
    
    #	Function to add post   
public function actionAdd() {
       
         $post = new Post();
        
       if (isset($_POST['ajax'])&& $_POST['ajax']==='post')
	            {
	                # code...
	                echo CActiveForm::validate($reguser);
	                Yii::app()->end();
	            }
        
        if (isset($_POST['Post'])) 
        {
        	
			 $post->attributes=$_POST['Post'];			 
			$post->image = CUploadedFile::getInstance($post,'image'); 
			$imgas=preg_split('/[.]+/',$post->image, -1, PREG_SPLIT_NO_EMPTY); 
							 $imgas[0]=mktime(date("s"),date("Y"),date("m"),date("d"));
		
        if ($post->validate('reg'))
			{
				 
				 $post->user_id = Yii::app()->user->id;
				 $post->id_tag_post=$_POST['taglist'];
				 $post->date=date('Y-m-d H:i:s');
				if ($post->image!="") {
					$post->image->saveAs('images/post/'.$imgas[0].".".$imgas[1]);
					$post->image=$imgas[0].".".$imgas[1];
				}
				 if($post->save())
				 {
				 	$this->Resize($post->image,125,125);
				 	$this->redirect('/admin/qpost/list');
				 }
			}
        	
        }
        	 $tagi=TagPost::model()->findAll();
               $list = CHtml::listData($tagi, 'id_tag_post', 'post_tag_name');
        $this->render("application.views.qpost.add",array('post'=>$post,'list'=>$list));
    }
    
	private function Delete($nameImg) 
		{
			if(@fopen('images/post/'.$nameImg, "r"))
			{
			 $imga_del=preg_split('/[.]+/',$nameImg, -1, PREG_SPLIT_NO_EMPTY);
				 		unlink('images/post/'.$nameImg);
			}
				 		
		}
	private function Resize($nameImage,$with,$height) 
		{
			
			
			Yii::app()->setComponents(array('imagemod'=>array('class'=>'application.extensions.imagemodifier.CImageModifier')));
				 		
				 $imga = Yii::app()->imagemod->load('images/post/'.$nameImage);
				 $imga->image_resize          = true;
				$imga->image_y         = $height;
				$imga->image_x         = $with;
				//$imga->file_new_name_body = $img->file_src_name;
				$imga->process('images/post/');
				if ($imga->processed) {					
					$imga->clean();
					$imga=preg_split('/[.]+/',$nameImage, -1, PREG_SPLIT_NO_EMPTY); 				
					
					rename('images/post/'.$imga[0]."_1.".$imga[1],'images/post/'.$nameImage);
				} else {
					echo 'error : ' . $img->error;
			}
		}

    
    
    
    
 # �������������� ������
    public function actionEdit() {
        $post_id = $_GET['id'];
	//Update news new style
	 
	$post=Post::model()->findByPk($post_id);
    if (isset($_POST['ajax'])&& $_POST['ajax']==='post')
	            {
	                # code...
	                echo CActiveForm::validate($reguser);
	                Yii::app()->end();
	            }
        
        if (isset($_POST['Post'])) 
        {
        	
			 $post->attributes=$_POST['Post'];			 
			 
        if ($post->validate('reg'))
			{
				 
			$image=  CUploadedFile::getInstance($post,'image');
				
				if($image!=null)
				 	{
				 		$inf_delete = Post::model()->findByPk($post_id);
				 		$this->Delete($inf_delete->image);
				 		 $post->image = CUploadedFile::getInstance($post,'image');
				 		 $imgas=preg_split('/[.]+/',$post->image, -1, PREG_SPLIT_NO_EMPTY); 
							 $imgas[0]=mktime(date("s"),date("Y"),date("m"),date("d"));
				 		$post->image->saveAs('images/post/'.$imgas[0].".".$imgas[1]);
				 		$post->image=$imgas[0].".".$imgas[1];
				 		$this->Resize($post->image,125,125);
				 		
				 	}
				 $post->id_tag_post=$_POST['taglist'];
				 //$usercv->date=date();

				 if($post->save())
				 {
				 	$this->redirect('/admin/qpost/list');
				 }
			}
        	
        }
       // $news = news::model()->findByPk($news_id);
        $tagi=Tagpost::model()->findAll();
       $Pos = new Post();
        $list = CHtml::listData($tagi, 'id_tag_post', 'post_tag_name');
        $this->render("application.views.qpost.edit",array('Pos'=>$Pos,'post' => $post, 'list'=>$list));
    }
    
    
    
    public function actionDel()
    {
   
        $post_id = $_GET['id'];
        
     	$inf_delete = Post::model()->findByPk($post_id);
        if ($inf_delete->image!="") 
        {
        	$this->Delete($inf_delete->image);
        }
        Post::model()->deleteByPk($post_id);
        Coments::model()->deleteAll('id_post=:id_post AND object=:object',array(':id_post' => $news_id,':object'=>'post'));
        $this->redirect('/admin/qpost/list/');
   
    	
    }
    
    
 #����� ���� ������ � �����������
   
    #������ ���������
    public function actionCategorylist() 
    {
    	$page_size =5; 
       // $tag = TagPost::model()->recently(0,$page_size)->findAll();
        $tag_count = TagPost::model()->count();
          $count_page=$tag_count/$page_size;
          
     	if(!isset($_GET['page']))
         {         	
         		$tag=TagPost::model()->recently(0,$page_size)->findAll();                   
         }
         else
           {
           	$page=$_GET['page'];
             $sdvig = $page_size*($page-1);
             $tag=TagPost::model()->recently( $sdvig,$page_size)->findAll();
           }
    	
    	
    	$this->render("application.views.qpost.categorylist",array('tag'=>$tag,'count_page'=>$count_page+1));
    }
    
    #�������������� ���������
    
    public function actionEditCategory() 
    {
    	 $tag_id = $_GET['id'];
	//echo $tag_id;
	$tag=TagPost::model()->findByPk($tag_id);
    if (isset($_POST['ajax'])&& $_POST['ajax']==='tagpost')
	            {
	                # code...
	                echo CActiveForm::validate($reguser);
	                Yii::app()->end();
	            }
        
        if (isset($_POST['TagPost'])) 
        {
        	
			 $tag->attributes=$_POST['TagPost'];			 
			 
        if ($tag->validate('reg'))
			{
				 
				// $usercv->user_id = Yii::app()->user->id;
				// $tag->id_tag=$_POST['taglist'];
				 //$usercv->date=date();

				 if($tag->save())
				 {
				 	$this->redirect('/admin/qpost/categorylist/');
				 }
			}
        	
        }
       // $news = news::model()->findByPk($news_id);
       
        $Tagi = new TagPost();
       
        $this->render("application.views.qpost.editcategory",array('tag' => $tag, 'Tagi'=>$Tagi));
    }
    
    #���������� ����� ���������
    public function actionAddCategory() 
    {
    	
    	 $tag = new TagPost();
        
       if (isset($_POST['ajax'])&& $_POST['ajax']==='tagpost')
	            {
	                # code...
	                echo CActiveForm::validate($reguser);
	                Yii::app()->end();
	            }
        
        if (isset($_POST['TagPost'])) 
        {
        	
			 $tag->attributes=$_POST['TagPost'];			 
			//print_r($_POST['News']);
		
        if ($tag->validate('reg'))
			{
				 
			
					
				 if($tag->save())
				 {
				 	$this->redirect('/admin/qpost/categorylist/');
				 }
			}
        	
        }
        	
        $this->render("application.views.qpost.addcategory",array('tag'=>$tag));
    	
    }
    
    #�������� ���������
 	public function actionDelCategory() {
        $tag_id = $_GET['id'];
        TagPost::model()->deleteByPk($tag_id);       
        $this->redirect('/admin/qpost/categorylist/');
    }
	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}